﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SuperX.Helper.ServiceResponse
{
    /// <summary>
    /// 数据类型转换
    /// </summary>
    public static class ApiResponseEX
    {
        public static ApiResponse<T> ToResponse<T>(this T data, string message = "")
        {
            return ApiResponse<T>.Success(message, data);
        }
    }

    /// <summary>
    /// 统一返回类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResponse<T>
    {
        public ResponseCode Code
        {
            get;
            set;
        }

        public string Message
        {
            get;
            set;
        }

        //返回数据
        public T Data
        {
            get;
            set;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ApiResponse<T> Success(string message = "", T data = default(T))
        {
            return new ApiResponse<T>
            {
                Message = message,
                Code = ResponseCode.Success,
                Data = data
            };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ApiResponse<T> Fail(string message = "")
        {
            return new ApiResponse<T>
            {
                Message = message,
                Code = ResponseCode.Failed
            };
        }

        public static ApiResponse<T> DeviceNotFound(string message = "")
        {
            return new ApiResponse<T>
            {
                Message = message,
                Code = ResponseCode.DeviceNotFound
            };
        }
    }

    /// <summary>
    /// 返回代码 Success：成功，Failed：失败
    /// </summary>
    public enum ResponseCode
    {
        Success = 0,
        Failed = 1,
        DeviceNotFound = 504,
    }

    public class ErrorMessage
    {
        public const string WriteNotSupport = "只读变量不支持写入";
        public const string DeviceNotFound = "没找到设备";
    }

    public class ErrorCode
    {
        public const int WriteNotSupport = 8;
    }
}